package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     **/
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        //先获得添加购物操作的UserId
        Long currentId = BaseContext.getCurrentId();
        //先将操作用户的id存储
        shoppingCart.setUserId(currentId);

        //判断添加的是单个的菜品还是 套餐
        Long dishId = shoppingCart.getDishId();
        //判断现在添加的是否已经在购物车中存在
        //先在这个位置添加条件的构建器  方便后期添加动态的查询条件
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, currentId);
        if (dishId != null) {
            //dishId不为空证明添加的是菜品
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
            //添加的是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart cartServiceOne = shoppingCartService.getOne(lqw);
        //判断cartserviceone是否为空
        if (cartServiceOne != null) {
            //不为空之前已经添加过 更新内部的num
            Integer number = cartServiceOne.getNumber();
            number = number + 1;
            cartServiceOne.setNumber(number);
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //之前并未添加
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(cartServiceOne);
    }

    /**
     * 查看购物车
     **/
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("查看购物车");
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //添加条件  只查看当前操作id的购物车
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        lqw.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return R.success(list);
    }

    /**
     * 点击减号 减少数量
     **/
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
//        // 首先要判断点击的是菜品还是套餐
//        Long dishId = shoppingCart.getDishId();
//        //构建条件添加器
//        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
//        //添加查询条件指定User
//        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
//        if (dishId != null) {
//            //此时点击的是对某一菜品的减量
//            lqw.eq(ShoppingCart::getDishId, dishId);
//        } else {
//            //dishid为空 此时是对购物车内 套餐的操作
//            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
//        }
//        //根据条件查询到的购物车对象就是要进行操作的对象  这个对象不会为空所有不用进行非空判断
//        ShoppingCart cartServiceOne = shoppingCartService.getOne(lqw);
//        Integer number = cartServiceOne.getNumber();
//        if (number > 1) {
//            //当操作时数量大于1时 不需要考虑数据的删除问题  对number进行减少即可
//            shoppingCart.setNumber(number-1);
//            shoppingCartService.updateById(shoppingCart);
//
//        }else if (number==1){
//            //数据为1的时候要对数据库内的数据进行删除
//            //lqw 内部的条件 也可以作为删除的条件来使用
//            shoppingCartService.remove(lqw);
//            shoppingCart=new ShoppingCart();
//            shoppingCart.setNumber(0);
//            shoppingCart.setUserId(BaseContext.getCurrentId());
//        }
        log.info("购物车内的信息:{}", shoppingCart);

        // 首先要判断点击的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        if (dishId != null) {
            //对菜品进行操作
            LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
            lambdaQueryWrapper.eq(ShoppingCart::getDishId, dishId);
            //得到菜品的购物车对象
            ShoppingCart cartServiceOne = shoppingCartService.getOne(lambdaQueryWrapper);
            //菜品购物车内菜品的数量
            Integer number = cartServiceOne.getNumber();
            if (number == 1) {
                //对数据进行删除操作
                shoppingCartService.removeById(cartServiceOne.getId());
                cartServiceOne.setNumber(0);
                return R.success(cartServiceOne);
            }
            //此时数据为大于1对数据的数量进行减少
            cartServiceOne.setNumber(number - 1);
            shoppingCartService.updateById(cartServiceOne);
            return R.success(cartServiceOne);

        } else {
            //此时是对套餐的操作
            LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
            lambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
            ShoppingCart cartServiceOne = shoppingCartService.getOne(lambdaQueryWrapper);
            //获得套餐的数量
            Integer number = cartServiceOne.getNumber();
            if (number == 1) {
                //删除操作
                shoppingCartService.removeById(cartServiceOne.getId());
                cartServiceOne.setNumber(0);
                return R.success(cartServiceOne);
            }
            //number>1  修改数量
            cartServiceOne.setNumber(number - 1);
            shoppingCartService.updateById(cartServiceOne);
            return R.success(shoppingCartService.getOne(lambdaQueryWrapper));
        }
    }


    /**
     * 清空购物车
     **/
    @DeleteMapping("/clean")
    public R<String> delete() {
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(lambdaQueryWrapper);

        return R.success("清空购物车成功!");
    }
}